//H5处理事件
mui.plusReady(function() {
	plus.webview.currentWebview().setStyle({
		scrollIndicator: 'none'
	});
	//判断当前网络状态
		get_connection();
	
		function get_connection() {
			var types = {};
			types[plus.networkinfo.CONNECTION_UNKNOW] = "Unknown connection";
			types[plus.networkinfo.CONNECTION_NONE] = "None connection";
			types[plus.networkinfo.CONNECTION_ETHERNET] = "Ethernet connection";
			types[plus.networkinfo.CONNECTION_WIFI] = "WiFi connection";
			types[plus.networkinfo.CONNECTION_CELL2G] = "Cellular 2G connection";
			types[plus.networkinfo.CONNECTION_CELL3G] = "Cellular 3G connection";
			types[plus.networkinfo.CONNECTION_CELL4G] = "Cellular 4G connection";
			if (types[plus.networkinfo.getCurrentType()] === "None connection" || types[plus.networkinfo.getCurrentType()] ===
				"Unknown connection") {
				plus.storage.setItem('network-status', 'false')
			} else {
				plus.storage.setItem('network-status', 'true')
			};
		};
		document.addEventListener("resume", function() {
			get_connection()
		}, false);
		document.addEventListener("netchange", function() {
			var nt = plus.networkinfo.getCurrentType();
			switch (nt) {
				case plus.networkinfo.CONNECTION_ETHERNET:
				case plus.networkinfo.CONNECTION_WIFI:
					plus.storage.setItem('network-status', 'true');
					mui.toast('您已切换至无线网络～');
					break;
				case plus.networkinfo.CONNECTION_CELL2G:
				case plus.networkinfo.CONNECTION_CELL3G:
				case plus.networkinfo.CONNECTION_CELL4G:
					plus.storage.setItem('network-status', 'true');
					mui.toast('您已切换至移动网络～');
					break;
				default:
					plus.storage.setItem('network-status', 'false');
					mui.toast('您已关闭网络～');
					break;
			};
		}, false);
}); 

var BASE = {
	//线上
	Host: 'http://106.112.128.46:8081/smartoilet/',
	//本地：
	// Host: 'http://10.101.36.169:8081/smartoilet/',
	// Host: 'http://10.101.36.79:8081/smartoilet/', // 董旭
	// 打开带返回键的默认窗口
	Open_window: function(url, WId, title, type, data) {
		if (plus.storage.getItem('network-status') === 'false') {
			mui.toast('当前暂无网络~');
			return false
		};
		if (!data) data = {};
		var webview_style = {
			left: 0,
			right: 0,
			top: 0,
			bottom: 0,
			position: 'static',
			popGesture: "close"
		};
		webview_style.titleNView = {
			type: type,
			autoBackButton: true,
			titleText: title,
			titleColor: '#FFFFFF',
			backgroundColor: "#007aff"
		};
		var webview = plus.webview.create(url, WId, webview_style, data);
		plus.webview.show(webview, "slide-in-right", 300);
	},
	// 打开自定义窗口
	Custom_window: function(url, WId, data, more, type) {
		if (plus.storage.getItem('network-status') === 'false') {
			mui.toast('当前暂无网络~');
			return false
		};
		if (!more) more = false;
		if (!data) data = {};
		if (!type) type = 'slide-in-right';
		mui.openWindow({
			url: url,
			id: WId,
			createNew: more,
			extras: data,
			show: {
				autoShow: true,
				aniShow: type
			}
		});
	},
	Getnew: function() {
		if (mui.os.ios) {
			return false
		};
		var _this = this;
		// 旧版1.1.0  新:1.1.1   110<111 更新   
		// 接口 里面返回版本号 和更新的路劲 
		plus.runtime.getProperty(plus.runtime.appid, function(old) {
			oldVer = old.version.replace(/\./g, '')
			_this.$http.get(BASE.Hosts + 'index/checkupdate').then(function(res) {
				if (res.status === 200 && res.data.error === 0) {
					res = res.data.message;
					if (!res) {
						plus.nativeUI.alert("没有获取版本信息！");
						return
					};
					var newVer = res.version.replace(/\./g, ''),
						upd_type = res.upd_type,
						update_url = parseInt(upd_type) ? res.wgturl : res.packurl;
					if (!oldVer || !newVer || (newVer <= oldVer)) {
						return
					};
					plus.nativeUI.confirm("发现新版本，是否现在更新？", function(e) {
						if (parseInt(e.index) === 0) {
							mui.toast('新版本努力下载中～');
							// 下载升级包
							plus.downloader.createDownload(update_url, {
								filename: "_doc/update/"
							}, function(d, status) {
								if (parseInt(status) === 200) {
									// 安装更新包
									plus.runtime.install(d.filename, {}, function() {
										mui.toast('开始安装新版本～');
									}, function(e) {
										plus.nativeUI.alert("应用安装失败[" + e.code + "]：" + e.message);
									});
								} else {
									plus.nativeUI.alert("升级包下载失败！");
								};
							}).start();
						};
					});
				} else {
					plus.nativeUI.alert("检测更新失败！");
				};
			}, function(res) {
				alert(JSON.stringify(res.data.message));
			});
		});
	}
};

var HTTP = {
	post: function(that, url, success_fun, error_fun, data, host) {
		data = JSON.stringify(data) || '';
		var Host = host || BASE.Host;
		that.$http.post(Host + url, {
			data: data
		}, {
			emulateJSON: true
		}).then(function(res, that) {
			plus.nativeUI.closeWaiting();
			// console.log("成功"+JSON.stringify(res))
			if (res.status === 200 && parseInt(res.data.code) === 0) {
				success_fun(res.data, that);
			} else {
				// mui.toast(res.data.mgs);
				console.log(JSON.stringify(res.data.mgs));
			};
		}, function(res) {
			// console.log("失败"+JSON.stringify(res)) 
			plus.nativeUI.closeWaiting();
			error_fun(res);
		});
	}
};
